package ${package.Controller};

import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import common.core.util.CommonUtil;
import common.core.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.ui.Model;

import ${config.injectionConfig.map.parent}.feign.${entity}Feign;
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};
</#if>

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */

@Slf4j
@RestController
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass}<${table.serviceName}, ${entity}> implements ${entity}Feign{
<#else>
public class ${table.controllerName} {
</#if>

    @Autowired
    private ${table.serviceName} i${entity}Service;

    /**
     * 列表页面表格数据
     * @param query 查询条件
     * @param page 分页对象
     * @return page 分页对象
     */
    @RequestMapping("/list")
    public Result listData(QueryWrapper<${entity}> query, Page<${entity}> page) {
        return Result.success(i${entity}Service.page(page, query));
    }

    /**
     * 跳转到修改页面
     * @param id 唯一主键
     * @param model Spring提供的绑定器
     * @return 修改页面
     */
    @RequestMapping("/edit")
    public Result editPage(String id, Model model) {
        if(CommonUtil.isEmpty(id)) {
            return Result.fail("id 不能为空");
        }
        ${entity} entity = this.i${entity}Service.getById(id);
        if(CommonUtil.isEmpty(entity)) {
            return Result.fail("数据不存在");
        }
        return Result.success(entity);
    }

    /**
     * 数据保存
     * @param entity 实体类
     * @return Result 统一结果对象
     */
    @RequestMapping("/saveOrUpdateEntity")
    public Result saveOrUpdateEntity(${entity} entity) {
        log.info("开始保存数据:{}" ,entity);
        this.i${entity}Service.saveOrUpdate(entity);
        return Result.success();
    }

    /**
     * 数据删除
     * @param queryWrapper 删除条件
     * @return Result 统一结果对象
     */
    @RequestMapping("/removed")
    public Result removed(QueryWrapper<${entity}> queryWrapper) {
        this.i${entity}Service.remove(queryWrapper);
        return Result.success();
    }

}
